/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Admin
 * Date: 2022-04-24
 * Time: 18:39
 */
public class TestDemo {
    /**
     *
     * @param array  时间复杂度为 O(n^2)
     */
    static void bubbleSort(int[] array) {

        for (int end = array.length; end > 0; end--) {

            boolean sorted = true;

            for (int i = 1; i < end; i++) {

                if (array[i - 1] > array[i]) {

                    Swap(array, i - 1, i);

                    sorted = false;

                }

            }

            if (sorted == true) {

                break;

            }

        }

    }

    /**
     *
     * @param array  空间复杂度为 O(1)
     */
    static void bubbleSort1(int[] array) {

        for (int end = array.length; end > 0; end--) {

            boolean sorted = true;

            for (int i = 1; i < end; i++) {

                if (array[i - 1] > array[i]) {

                    Swap(array, i - 1, i);

                    sorted = false;

                }

            }




            if (sorted == true) {

                break;

            }

        }

    }

    public static void main(String[] args) {
        int[] ret = {1,2,3,4,5,6,7};
        bubbleSort( ret);
    }
}